#ifndef __JUDGE_H__
#define __JUDGE_H__

#include "Player.hpp"
#include "ChessBoard.hpp"

class Judge
{
public:
	bool isWin(Player* player)
	{
		bool win = false;
		string chessColor = player->getColor();

		win = isHorizontalWin(chessColor);

		if (win)
		{
			return true;
		}

		win = isVerticalWin(chessColor);

		if (win)
		{
			return true;
		}

		win = isUpHillWin(chessColor);

		if (win)
		{
			return true;
		}

		win = isDownHillWin(chessColor);

		if (win)
		{
			return true;
		}

		return win;
	}

	bool isHorizontalWin(const string& chessColor)
	{
		int count = 0;

		ChessBoard* chessBoard = ChessBoard::getChessBoard();
		int curLine = chessBoard->getCurrentLine();
		int curColumn = chessBoard->getCurrentColumn();

		// right
		for (int i = 0; i < 5; i++)
		{
			if (chessBoard->isSameColorChess(chessColor, curLine, curColumn + i))
			{
				count++;
			}
			else
			{
				break;
			}
		}

		if (count >= 5)
		{
			return true;
		}

		for (int i = 0; i < 5; i++)
		{
			// left
			if (chessBoard->isSameColorChess(chessColor, curLine, curColumn - i))
			{
				count++;
			}
			else
			{
				break;
			}
		}
		return (count - 1) >= 5 ? true : false;
	}

	bool isVerticalWin(const string& chessColor)
	{
		int count = 0;

		ChessBoard* chessBoard = ChessBoard::getChessBoard();
		int curLine = chessBoard->getCurrentLine();
		int curColumn = chessBoard->getCurrentColumn();

		// up
		for (int i = 0; i < 5; i++)
		{
			if (chessBoard->isSameColorChess(chessColor, curLine - i, curColumn))
			{
				count++;
			}
			else
			{
				break;
			}
		}

		if (count >= 5)
		{
			return true;
		}

		// down
		for (int i = 0; i < 5; i++)
		{
			if (chessBoard->isSameColorChess(chessColor, curLine + i, curColumn))
			{
				count++;
			}
			else
			{
				break;
			}
		}
		return (count - 1) >= 5 ? true : false;
	}

	bool isUpHillWin(const string& chessColor)
	{
		int count = 0;

		ChessBoard* chessBoard = ChessBoard::getChessBoard();
		int curLine = chessBoard->getCurrentLine();
		int curColumn = chessBoard->getCurrentColumn();

		// up
		for (int i = 0; i < 5; i++)
		{
			if (chessBoard->isSameColorChess(chessColor, curLine - i, curColumn + i))
			{
				count++;
			}
			else
			{
				break;
			}
		}

		if (count >= 5)
		{
			return true;
		}

		// down
		for (int i = 0; i < 5; i++)
		{
			if (chessBoard->isSameColorChess(chessColor, curLine + i, curColumn - i))
			{
				count++;
			}
			else
			{
				break;
			}
		}
		return (count - 1) >= 5 ? true : false;
	}

	bool isDownHillWin(const string& chessColor)
	{
		int count = 0;

		ChessBoard* chessBoard = ChessBoard::getChessBoard();
		int curLine = chessBoard->getCurrentLine();
		int curColumn = chessBoard->getCurrentColumn();

		// up
		for (int i = 0; i < 5; i++)
		{
			if (chessBoard->isSameColorChess(chessColor, curLine - i, curColumn - i))
			{
				count++;
			}
			else
			{
				break;
			}
		}

		if (count >= 5)
		{
			return true;
		}

		// down
		for (int i = 0; i < 5; i++)
		{
			if (chessBoard->isSameColorChess(chessColor, curLine + i, curColumn + i))
			{
				count++;
			}
			else
			{
				break;
			}
		}
		return (count - 1) >= 5 ? true : false;
	}
};
#endif // !__JUDGE_H__
